﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_emp_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlColList.DataSource = dal.GetColtd(int.Parse(Session["ColtdId"].ToString()), true);
            this.ddlColList.DataBind();

            this.ddlDept.DataSource = dal.GetDept(int.Parse(this.ddlColList.SelectedValue), null, true);
            this.ddlDept.DataBind();
        }

        private void bindDeptDDl()
        {
            DataAccess.PmData dal = new DataAccess.PmData();

            this.ddlDept.DataSource = dal.GetDept(int.Parse(this.ddlColList.SelectedValue), null, true);
            this.ddlDept.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_face_m_emp(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetEmp(int.Parse(this.hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        this.ddlColList.ClearSelection();
                        this.ddlDept.ClearSelection();
                        this.ddlColList.Items.FindByValue(dt.Rows[0]["self_coltd_code"].ToString()).Selected = true;
                        bindDeptDDl();
                        this.ddlDept.Items.FindByValue(dt.Rows[0]["self_dep_code"].ToString()).Selected = true;
                        this.ddlRole.Items.FindByValue(dt.Rows[0]["roleid"].ToString()).Selected = true;
                    }
                    catch { }
                    this.txtName.Text = dt.Rows[0]["self_empname"].ToString();
                    //this.txtSort.Text = dt.Rows[0]["self_sortid"].ToString();
                    //this.hidLayer.Value = dt.Rows[0]["self_layer"].ToString();
                    //this.hidCode.Value = dt.Rows[0]["self_all_code"].ToString();
                    //this.hidUpId.Value = dt.Rows[0]["up_dep_code"].ToString();
                    //this.hidColtdId.Value = dt.Rows[0]["self_coltd_code"].ToString();
                }
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            string sColtd = this.ddlColList.SelectedValue;
            string sDept = this.ddlDept.SelectedValue;
            string sName = this.txtName.Text.Trim();
            string sRole = this.ddlRole.SelectedValue;
            if (sName == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "姓名不能为空！");
                return;
            }
            if (sDept == string.Empty || sColtd==string.Empty)
            {
                PM.Web.Common.JsAlert(this, "机构或部门不能为空！");
                return;
            }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_emp(int.Parse(sColtd), int.Parse(sDept), 0, 0, sName, 0, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, int.Parse(sRole), bool.Parse(this.ddlStatus.SelectedValue)))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.modify_face_m_emp(int.Parse(this.hidId.Value), int.Parse(sColtd), int.Parse(sDept), 0, 0, sName, 0, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, int.Parse(sRole), bool.Parse(this.ddlStatus.SelectedValue)))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }

        protected void ddlColList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindDeptDDl();
        }
    }
}